package com.wxdc.nmgyt.nmgytwydc.presenter;

import com.wxdc.nmgyt.nmgytwydc.base.RxPersenter;
import com.wxdc.nmgyt.nmgytwydc.http.RetrofitHelper;
import com.wxdc.nmgyt.nmgytwydc.model.CodeBean;
import com.wxdc.nmgyt.nmgytwydc.model.UserLoginBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.UserLoginResultBean;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.LoginContract;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/4/19.
 */

public class LoginPersenter extends RxPersenter<LoginContract.View> implements LoginContract.Persenter {
    private RetrofitHelper mRetrofitHelper;

    @Inject
    public LoginPersenter(RetrofitHelper retrofitHelper) {
        this.mRetrofitHelper = retrofitHelper;
    }

    @Override
    public void loginServer(String name, String pwd) {
        UserLoginBean userLoginBean = new UserLoginBean();
        userLoginBean.setUserName(name);
        userLoginBean.setPasswordHash(pwd);
        mRetrofitHelper.userLoginService(userLoginBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserLoginResultBean>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        addDisposable(disposable);
                    }

                    @Override
                    public void onNext(UserLoginResultBean resultBean) {
                        mView.loginResult(resultBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("请求失败,请检查网络接口");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


}
